package com.neuedu.corejava.ch13.homework;

/**
 * 20190814
 * 
 * JAVA多线程编程。 用JAVA写一个多线程程序，如写四个线程，二个加1，二个对一个变量减一，输出。
 * @author admin
 *
 */
public class SynHomework {
	
	private int j=0;
	
	public synchronized void inc(){
		j++;
		System.out.println(Thread.currentThread().getName()+"-inc"+j);
	}
	
	public synchronized void dec(){
		j--;
		System.out.println(Thread.currentThread().getName()+"-dec"+j);
	}
	
	class Inc implements Runnable{
		public void run(){
			for(int i=0;i<5;i++){
				inc();
			}
		}
	}
	
	class Dec implements Runnable{
		public void run(){
			for(int i=0;i<5;i++){
				dec();
			}
		}
	}
	
	public static void main(String[] args) {
		SynHomework syn=new SynHomework();
		Inc inc=syn.new Inc();
		Dec dec=syn.new Dec();
		for(int i=0;i<2;i++){
			Thread thread1=new Thread(inc);
			thread1.start();
			Thread thread2=new Thread(dec);
			thread2.start();
		}

	}

}
